package com.saikuo.service.dao.impl;

import com.saikuo.service.constant.CouponStatus;
import com.saikuo.service.dao.ICouponDao;
import com.saikuo.service.vo.CouponView;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created by shanchangyu on 16/11/03.
 */
@Repository
public class CouponDaoImpl extends JdbcDaoSupport implements ICouponDao {

    private static final BeanPropertyRowMapper<CouponView> mapper = new
            BeanPropertyRowMapper<>(CouponView.class);

    @Autowired
    CouponDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
    }


    private static final String GET_UNNORMAL_COUPON = "select u.id,c.title,c.start_time,c.end_time,c.explanation,c.pic_url,c.price,c.condition,u.status " +
            " from y_user_coupon u  left join y_coupon c " +
            " on u.coupon_id=c.id where u.user_id=? and u.status !=0 order by c.id desc limit ?,?";

    private static final String GET_NORMAL_COUPON = "select u.id, c.title,c.start_time,c.end_time,c.explanation,c.pic_url,c.price,c.condition,u.status " +
            " from y_user_coupon u  left join y_coupon c " +
            " on u.coupon_id=c.id where u.user_id=? and u.status=? order by c.id desc limit ?,?";

    private static final String UPDATE_UNNORMAL_COUPON = "update y_user_coupon set status =2 where user_id =? and coupon_id in (select id from y_coupon where end_time <now())";

    @Override
    public List<CouponView> getUserCoupon(int userId, int status, int start, int limit) {
        List<CouponView> result = null;
        //将过期优惠标记
        this.getJdbcTemplate().update(UPDATE_UNNORMAL_COUPON, new Object[]{userId});
        if (status == CouponStatus.NORMAL.getValue()) {
            result = this.getJdbcTemplate().query(GET_NORMAL_COUPON, new Object[]{userId, status, start, limit}, mapper);
        } else {
            result = this.getJdbcTemplate().query(GET_UNNORMAL_COUPON, new Object[]{userId, start, limit}, mapper);
        }
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result;
    }

    private static final String GET_NORMAL_COUPON_ACCORD_PRICE = "select u.id,c.title,c.start_time,c.end_time,c.explanation,c.pic_url,c.price,c.condition,u.status " +
            " from y_user_coupon u  left join y_coupon c " +
            " on u.coupon_id=c.id where u.user_id=? and ?>=c.condition and u.status=0 order by c.id desc limit ?,?";

    @Override
    public List<CouponView> getUserCouponAccordPrice(int userId, Double price, int start, int limit) {
        //将过期优惠标记
        this.getJdbcTemplate().update(UPDATE_UNNORMAL_COUPON, new Object[]{userId});
        List<CouponView> result = this.getJdbcTemplate().query(GET_NORMAL_COUPON_ACCORD_PRICE, new Object[]{userId, price, start, limit}, mapper);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result;
    }

    private static final String GET_COUPON_BY_USER_COUPON_ID = "select u.id,c.title,c.start_time,c.end_time,c.explanation,c.pic_url,c.price,c.condition,u.status " +
            " from y_user_coupon u  left join y_coupon c " +
            " on u.coupon_id=c.id where u.id=? ";

    @Override
    public CouponView getCouponByUserCouponId(int userCouponId) {
        List<CouponView> result = this.getJdbcTemplate().query(GET_COUPON_BY_USER_COUPON_ID, new Object[]{userCouponId}, mapper);
        if (CollectionUtils.isNotEmpty(result)) {
            return result.get(0);
        }
        return null;
    }

    private static final String UPDATE_NORMAL_COUPON = "update y_user_coupon set status =? where id=?";

    @Override
    public boolean updateUsedCoupon(int id, int status) {
        return 1 == this.getJdbcTemplate().update(UPDATE_NORMAL_COUPON, new Object[]{status, id});
    }

}
